export default {
  data() {
    return {
      carImageS: [], //图片list
      indicatorDots: false, //是否显示指示点
      autoplay: false, //停止自动播放
      carIndex: 0, //当前传入的index
      showimg: false, //图片选择.官方,平台,相册,相机
      Seriesid: 0, //系列id
      currentCarIndex: 0, //当前的index
      currentCarImg: '', //当前的图片
      currentImgs: '', //选择后，返回的图片

      // 上传图片
      imgPolicy: '',
      imgAuthorization: '',
      imgHost: '',

      faceimg: '', //返回图片地址
    }
  },
  onLoad(carvule) {
    this.carImageS = JSON.parse(decodeURIComponent(carvule.outsideimgwg))
    this.carIndex = Number(carvule.indexs)
    this.Seriesid = carvule.Seriesid

    // 选择后的图片地址
    uni.$on('currentImgs', (data) => {
      Object.assign(this.currentImgs, data)
      this.currentImgs = data.currentimg
      for (var i = 0; i < this.carImageS.length; i++) {
        if (i === this.carIndex) {
          this.carImageS[this.carIndex].img = this.currentImgs
        }
      }
    })
  },
  mounted() {
    this.getImageConfig()
  },
  methods: {
    // 返回
    fanhui() {
      //图片地址
      uni.$emit('carImageS', {
        carImageS: this.carImageS,
      })
      uni.navigateBack()
      uni.$off('currentImgs')
    },
    //替换
    replace() {
      // 判断是否是预览发布车品的图片 ===> git用户: donte添加的
      // 如果是车品进来预览图片的，不需要弹出底部选项
      if (this.Seriesid === 'carGoods') {
        this.uploadImage()
      } else {
        this.showimg = true //打开上传图片弹框
      }
    },
    //删除
    deleteC() {
      if (this.carImageS.length > 1) {
        this.carImageS.splice(this.carIndex, 1) //取消
      } else {
        this.$refs.uToast.show({
          title: '请至少保留一张图片',
          type: 'warning',
          url: '/pages/user/index',
        })
      }
    },
    //切换后滑块变化
    carchangge(event) {
      // 当前图片位置index
      this.carIndex = event.detail.current
      for (var i = 0; i < this.carImageS.length; i++) {
        if (i === this.carIndex) {
          //当前图片地址
          this.currentCarImg = this.carImageS[this.carIndex].img
          console.log(this.currentCarImg)
        }
      }
    },
    // 官方图
    UploadimgGF(indexs) {
      uni.navigateTo({
        url:
          '/pages_carSource/appearancePicture/appearancePicture?Seriesid=' +
          this.Seriesid +
          '&&type=' +
          indexs,
      })
      this.showimg = false //关闭上传图片弹框
    },
    //平台图
    UploadimgPT(indexs) {
      uni.navigateTo({
        url:
          '/pages_carSource/appearancePicture/appearancePicture?Seriesid=' +
          this.Seriesid +
          '&&type=' +
          indexs,
      })
      this.showimg = false //关闭上传图片弹框
    },
    //获取图片上传配置信息
    async getImageConfig() {
      try {
        const res = await this.$api.user.getImageConfig()
        this.imgAuthorization = res.data.auth
        this.imgPolicy = res.data.policy
        this.imgHost = res.data.host
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 相册
    UploadIDcardImg() {
      this.uploadImage()
    },
    //拍摄
    UploadIDcardpaishe() {
      this.uploadImage()
    },
    //选择图片
    uploadImage() {
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        success: (result) => {
          uni.showLoading({
            title: '图片上传中~',
          })
          //请求接口获取上传文件的参数
          this.$api.files
            .uploadImg({
              filePath: result.tempFilePaths[0],
            })
            .then((res) => {
              console.log(res)
              var abc = res.baseUrl + res.url
              this.FanHUIimgs(abc)
              uni.hideLoading()
            })
          // uni.request({
          //   url: 'https://ztcapi.yzsheng.com/UpYun/UploadReceiptImgUpyunConfig',
          //   method: 'POST',
          //   data: {
          //     policy: _this.imgPolicy,
          //     authorization: _this.imgAuthorization,
          //     fname: Math.random() * 10,
          //     sFile: result.tempFilePaths[0], // 图片流
          //   },
          //   success: ConfigResult => {
          //     var UPYUN = ConfigResult
          //     var yunUrl = UPYUN['data'].host
          //     //上传文件
          //     uni.uploadFile({
          //       url: yunUrl,
          //       // files: result.tempFiles,
          //       formData: {
          //         policy: UPYUN['data'].policy,
          //         authorization: UPYUN['data'].auth
          //       },
          //       filePath: result.tempFilePaths[0],
          //       name: 'file',
          //       success(res) {
          //         if (JSON.parse(res.data).code === 200) {
          //           var abc = 'https://ztc.yzsheng.com/' + JSON.parse(res.data).url
          //           _this.FanHUIimgs(abc)
          //         }
          //         uni.hideLoading()
          //       },
          //       fail(er) {}
          //     })
          //     uni.hideLoading()
          //   }
          // })
        },
        error: function (e) {},
      })
    },
    //上传图片赋值
    FanHUIimgs(res) {
      this.showimg = false //关闭上传图片弹框
      this.currentImgs = res
      for (var i = 0; i < this.carImageS.length; i++) {
        if (i === this.carIndex) {
          this.carImageS[this.carIndex].img = this.currentImgs
        }
      }
    },
  },
}
